package com.Servlet;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@WebServlet("/returnUrl")
public class rentunUrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        try {
            //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
            //签名方式：SIGN_TYPE = "RSA2"
            //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址:NOTIFY_URL = "http://fafa-xxxx.natapp1.cc/pay/returnUrl"
            //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址:RETURN_URL = "成功返回项目地址"
            System.out.println("=================================同步回调=====================================");
            Map<String, String> params = new HashMap<String, String>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                }
                // 乱码解决，这段代码在出现乱码时使用
                valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
                params.put(name, valueStr);
            }

            System.out.println(params);//查看参数都有哪些
            boolean signVerified = AlipaySignature.rsaCheckV1(params, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoVEJqyYasm3j+e07/eb0/IKWiFUQvJoXfvwaVybfOYiocbrw6Vvf4BGnSf1FMijFBnFGFFumTr0OUDrPO1PsiLspLsw4VbXIF0U9JlEs+nmoTxeppecDghgEQhkp51Nz4e2IMO6F7CVD2aiDzmri7ymtWNRxx3xFGjcT0uSO7/OubHPpFtPDrSmd2M727g3sYR+u10nw5Yszq2JOgm6TDBrbPrjoVNb09APCwP0pPZp5NJedyTlxbLillPebJCNaHiS6OO+diSc9tz1khdkrvx/iwMX0unlKHHoRiI7d+6YZ8TCCWsl5yI0Fy+tFpP6jBbGzyw+shAz1lpNiW3PhGQIDAQAB", "utf-8", "RSA2"); // 调用SDK验证签名
//验证签名通过
            if(signVerified){
                // 商户订单号
                String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

                // 支付宝交易号
                String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");

                // 付款金额
                String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");

                System.out.println("商户订单号="+out_trade_no);
                System.out.println("支付宝交易号="+trade_no);
                System.out.println("付款金额="+total_amount);

                //支付成功，修复支付状态
                // payService.updateById(Integer.valueOf(out_trade_no));
                out.print("ok"); //跳转付款成功页面
            }else{
                out.print("no");//跳转付款失败页面
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }
}
